from apps.merchant.activity.dao import ActivityDao
import json
from decimal import Decimal
from apps.system.lessee.dao import LesseeDao

def countProfitForFranchiseeFun(params):
    try:
        print('计算加盟店线上下单利润', params)
        lessee = LesseeDao().getLessee()
        franchiseeParams = json.loads(lessee.franchiseeParams)
        sellingPrice = Decimal(params.get('sellingPrice'))
        costPrice = Decimal(params.get('costPrice'))
        # 购买者贡献点
        sendContributionValue = Decimal(sellingPrice / int(franchiseeParams.get('selfSendContributionValueRate')))
        # 上级贡献点
        verticalSendContributionValue = Decimal(sendContributionValue / 2)
        # 赠送购买者锁定积分
        selfSendIncomesLocked = sellingPrice * Decimal(franchiseeParams.get('selfSendIncomesLockedRate'))
        # 加盟商让利，也就是净值
        discountsAward = sellingPrice - costPrice
        # 商家贡献点
        shopSendContributionValue = Decimal(discountsAward / int(franchiseeParams.get('selfSendContributionValueRate')))
        # 加盟店余额
        shop_balance = costPrice
        # 直推购物积分
        verticalSendIncomes = discountsAward * Decimal(franchiseeParams.get('verticalSendIncomesRate'))
        # 日释放池
        dayAward = discountsAward * Decimal(franchiseeParams.get('dayAwardRate'))
        # 月释放池
        monthAward = discountsAward * Decimal(franchiseeParams.get('monthAwardRate'))
        # 运营补贴
        operationAward = discountsAward * Decimal(franchiseeParams.get('operationAwardRate'))
        # 商户推荐人奖励
        shopParentAward = discountsAward * Decimal(franchiseeParams.get('shopParentAwardRate'))
        # 创业基金
        helpFund = discountsAward * Decimal(franchiseeParams.get('helpFundRate'))
        # 赠送加盟店锁定积分
        shopSendIncomesLocked = discountsAward * Decimal(franchiseeParams.get('shopSendIncomesLockedRate'))
        # 毛利润
        grossProfit = discountsAward - (verticalSendIncomes + dayAward + monthAward + operationAward + shopParentAward + helpFund)

        profitObj = {
            'sendContributionValue': str(sendContributionValue.quantize(Decimal('0.00'))),
            'verticalSendContributionValue': str(verticalSendContributionValue.quantize(Decimal('0.00'))),
            'shopSendContributionValue': str(shopSendContributionValue.quantize(Decimal('0.00'))),
            'selfSendIncomesLocked': str(selfSendIncomesLocked.quantize(Decimal('0.00'))),
            'shopSendIncomesLocked': str(shopSendIncomesLocked.quantize(Decimal('0.00'))),
            'shop_balance': str(shop_balance.quantize(Decimal('0.00'))),
            'verticalSendIncomes': str(verticalSendIncomes.quantize(Decimal('0.00'))),
            'dayAward': str(dayAward.quantize(Decimal('0.00'))),
            'operationAward': str(operationAward.quantize(Decimal('0.00'))),
            'shopParentAward': str(shopParentAward.quantize(Decimal('0.00'))),
            'monthAward': str(monthAward.quantize(Decimal('0.00'))),
            'helpFund': str(helpFund.quantize(Decimal('0.00'))),
            'grossProfit': str(grossProfit.quantize(Decimal('0.00'))),
        }
        return profitObj
    
    except Exception as e:
        print('计算加盟店奖励出错', e)
        return


def countProfitFun(params):
    activity = ActivityDao().findOneByObjId(params.get('activityId'))
    sellingPrice = Decimal(params.get('sellingPrice'))
    costPrice = Decimal(params.get('costPrice'))

    activityParamsObj = json.loads(activity.activityParams)
    # if params.get('scene') == 'giftSection':
    #     # 设置购买者锁仓比例为3倍
    #     activityParamsObj['selfSendIncomesLockedRate'] = 3
    # 如果是礼包商品且金额大于等于30000，也送3倍锁仓
    if params.get('scene') == 'generay' and activity.key == 'giftpacks' and sellingPrice * Decimal(params.get('payCount')) >= 30000:
        activityParamsObj['selfSendIncomesLockedRate'] = 3
    if activity.key == 'generalGoods' or activity.key == 'giftpacks':
        # 购买者贡献点
        sendContributionValue = sellingPrice / Decimal(activityParamsObj.get('selfSendContributionValueRate'))
        # 上级贡献点
        verticalSendContributionValue = Decimal(sendContributionValue / 2)
        # 赠送购买者锁仓
        selfSendIncomesLocked = sellingPrice* Decimal(activityParamsObj.get('selfSendIncomesLockedRate'))
        # 直推赠送购物积分
        verticalSendIncomes = sellingPrice * Decimal(activityParamsObj.get('verticalSendIncomesRate'))
        # 日分红
        dayAward = sellingPrice * Decimal(activityParamsObj.get('dayAwardRate'))
        # 运营补贴
        operationAward = sellingPrice * Decimal(activityParamsObj.get('operationAwardRate'))
        # 净值
        netValue = sellingPrice - (costPrice + verticalSendIncomes + dayAward + operationAward)
        if netValue < 0:
            netValue = 0
        # 月分红
        monthAward = netValue * Decimal(activityParamsObj.get('monthAwardRate'))
        # 供应链奖励
        supplyAward = sellingPrice * Decimal(activityParamsObj.get('supplyAwardRate'))
        # 创业基金
        helpFund = sellingPrice * Decimal(activityParamsObj.get('helpFundRate'))
        # 毛利润
        grossProfit = netValue - monthAward - supplyAward - helpFund
        profitObj = {
            'sendContributionValue': str(sendContributionValue.quantize(Decimal('0.00'))),
            'verticalSendContributionValue': str(verticalSendContributionValue.quantize(Decimal('0.00'))),
            'selfSendIncomesLocked': str(selfSendIncomesLocked.quantize(Decimal('0.00'))),
            'verticalSendIncomes': str(verticalSendIncomes.quantize(Decimal('0.00'))),
            'dayAward': str(dayAward.quantize(Decimal('0.00'))),
            'operationAward': str(operationAward.quantize(Decimal('0.00'))),
            'monthAward': str(monthAward.quantize(Decimal('0.00'))),
            'supplyAward': str(supplyAward.quantize(Decimal('0.00'))),
            'helpFund': str(helpFund.quantize(Decimal('0.00'))),
            'grossProfit': str(grossProfit.quantize(Decimal('0.00'))),
        }
        return profitObj
    elif activity.key == 'discountGoods':
        # 购买者贡献点
        sendContributionValue = sellingPrice / Decimal(activityParamsObj.get('selfSendContributionValueRate'))
        # 上级贡献点
        verticalSendContributionValue = Decimal(sendContributionValue / 2)
        # 赠送购买者锁仓
        selfSendIncomesLocked = sellingPrice* Decimal(activityParamsObj.get('selfSendIncomesLockedRate'))
        # 净值
        netValue = sellingPrice - costPrice
        if netValue < 0:
            netValue = 0
        # 直推赠送购物积分
        verticalSendIncomes = netValue * Decimal(activityParamsObj.get('verticalSendIncomesRate'))
        # 日分红
        dayAward = netValue * Decimal(activityParamsObj.get('dayAwardRate'))
        # 运营补贴
        operationAward = netValue * Decimal(activityParamsObj.get('operationAwardRate'))
        # 月分红
        monthAward = netValue * Decimal(activityParamsObj.get('monthAwardRate'))
        # 供应链奖励
        supplyAward = netValue * Decimal(activityParamsObj.get('supplyAwardRate'))
        # 创业基金
        helpFund = netValue * Decimal(activityParamsObj.get('helpFundRate'))
        # 毛利润
        grossProfit = netValue - (verticalSendIncomes + operationAward + dayAward + monthAward + helpFund + supplyAward)
        profitObj = {
            'sendContributionValue': str(sendContributionValue.quantize(Decimal('0.00'))),
            'verticalSendContributionValue': str(verticalSendContributionValue.quantize(Decimal('0.00'))),
            'selfSendIncomesLocked': str(selfSendIncomesLocked.quantize(Decimal('0.00'))),
            'verticalSendIncomes': str(verticalSendIncomes.quantize(Decimal('0.00'))),
            'dayAward': str(dayAward.quantize(Decimal('0.00'))),
            'operationAward': str(operationAward.quantize(Decimal('0.00'))),
            'monthAward': str(monthAward.quantize(Decimal('0.00'))),
            'supplyAward': str(supplyAward.quantize(Decimal('0.00'))),
            'helpFund': str(helpFund.quantize(Decimal('0.00'))),
            'grossProfit': str(grossProfit.quantize(Decimal('0.00'))),
        }
        return profitObj